package com.sjtucook;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SecondMenuActivity extends Activity {
	
	public static final String Second = "second";
	private Button search;
	private Button favorite;
	private Button toprated;
	private Button history;
	private Button back;
	private LinearLayout content;

	private String TABLE_NAME;
	private String COLUMN_NAME;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.content);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		content = (LinearLayout) findViewById(R.id.center_pane);
		// get bundle from last activity
		Bundle bundle = this.getIntent().getExtras();
		TABLE_NAME = bundle.getString(DBAdapter.KEY_TABLE_NAME);
		COLUMN_NAME = bundle.getString(DBAdapter.KEY_COLUMN_NAME);
		// search button
		this.search = (Button) findViewById(R.id.tittle_bt);
		this.search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onSearchRequested();
			}
		});
		// favorite button
		this.favorite = (Button) findViewById(R.id.bottom_button_1);
		this.favorite.setText(R.string.FAVORITE);
		this.favorite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SecondMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.FAVORITE);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		});
		// toprated button
		this.toprated = (Button) findViewById(R.id.bottom_button_2);
		this.toprated.setText(R.string.TOP_RATED);
		this.toprated.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SecondMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.RATE);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		});
		// history button
		this.history = (Button) findViewById(R.id.bottom_button_3);
		this.history.setText(R.string.HISTORY);
		this.history.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SecondMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.HISTORY);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		});
		// update button
		this.back = (Button) findViewById(R.id.bottom_button_4);
		this.back.setText(R.string.BACK);
		this.back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}

		});

		// get CookApplication object
		CookApplication currentApp = (CookApplication) this
				.getApplicationContext();
		// db query
		ArrayList<HashMap<String, Object>> sourceList = currentApp
				.getDbadapter().querySecondSort(TABLE_NAME);

		if (sourceList.size() > 0) {
			// fill the contnet with a gridview
			GridView gridView = new GridView(this);
			gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			gridView.setNumColumns(GridView.AUTO_FIT);
			gridView.setScrollBarStyle(GridView.SCROLLBARS_OUTSIDE_OVERLAY);
			gridView.setSelector(R.color.white);
			content.addView(gridView);
			// 绑定数据源
			gridView.setAdapter(new SimpleAdapter(this, sourceList,
					R.layout.griditem, new String[] { DBAdapter.KEY_IMAGE,
							DBAdapter.KEY_NAME }, new int[] { R.id.ItemImage,
							R.id.ItemText }));
			// 绑定事件触发
			gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					HashMap<String, Object> hashMap = (HashMap<String, Object>) parent
							.getItemAtPosition(position);
					Intent intent = new Intent(SecondMenuActivity.this,
							DishListActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString(DBAdapter.KEY_ID,
							(String) hashMap.get(DBAdapter.KEY_ID));
					bundle.putString(DBAdapter.KEY_COLUMN_NAME, COLUMN_NAME);
					bundle.putString(DishListActivity.DISHSORT,(String) hashMap.get(DBAdapter.KEY_NAME));
					bundle.putString(DishListActivity.FLAG, SecondMenuActivity.Second);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
		}else{
			//no sort category
			TextView textView = new TextView(this);
			textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			textView.setGravity(Gravity.CENTER);
			textView.setText("No Item Found");
			content.addView(textView);
		}
	}
}
